const inquirer = require('inquirer')
const chalk = require('chalk')
const core = require('./core')
const { getConfig } = require('./config')

module.exports = async function () {
  console.log()
  console.log(chalk.green('欢迎使用202103地大网络教育刷课工具'))
  console.log()
  console.log(chalk.white('本工具刷课原理是，本工具会模拟一个能自动操作的浏览器访问地大网站并播放视频'))
  console.log()
  console.log(chalk.white('提前准备好学号和身份证号，用于登录地大网站，以及网络通畅'))
  console.log()

  const { username,password,headless } = await getConfig()
  await inquirer
    .prompt([
      {
        name: "readay",
        type: "list",
        message: "准备好了吗？",
        choices: [
          "Yes",
          "No"
        ]
      },
    ])
    .then((res) => {
      if (res.readay === 'Yes') {
        return
      }
      console.log(chalk.red('正在退出工具...'))
      return Promise.reject('正在退出工具...')
    })
  await inquirer
    .prompt([
      {
        name: "username",
        type: "input",
        message: "请输入学号",
        default: username,
        validate (value) {
          if (value.toString().length === 13) {
            return true
          }
          return "请输入13位学号" + value.toString()
        }
      },
      {
        name: "password",
        type: "password",
        default: password,
        message: "请输入身份证号",
        validate (value) {
          if (value.toString().length === 18) {
            return true
          }
          return "请输入18位身份证号"
        }
      },
      {
        name: "headless",
        type: "list",
        default: headless,
        choices: [
          "Yes",
          "No"
        ],
        message: "是否显示刷课的浏览器"
      },
    ])
    .then((res) => {
      return core(res)
    })
}